package vip.xiaonuo.bi.core.datasource.util;

import java.util.Random;

/**
 * @auther: 顾育司
 * @Description 用于每次生成都不一样的id
 * @date: 2024/1/9
 */
public class IdUtils {
    private static final char[] CHARS = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
    private static final char[] NUMBERS = "0123456789".toCharArray();
    private static final int RANDOM_STRING_LENGTH = 20;

    /**
     * 随机id
     * @return
     */
    public static String getRandomID() {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        // 保证为字母开头
        sb.append(CHARS[random.nextInt(25)]);


        for (int i = 1; i < RANDOM_STRING_LENGTH; i++) {
            char randomChar = CHARS[random.nextInt(CHARS.length)];
            sb.append(randomChar);
        }
        return sb.toString();
    }

    /**
     * 主要是doris动态表名专用
     * @param prefix
     * @return
     */
    public static String getRandomID(String prefix) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        // 保证为字母开头
        sb.append(prefix+"_");
        for (int i = 1; i < RANDOM_STRING_LENGTH; i++) {
            char randomChar = NUMBERS[random.nextInt(NUMBERS.length)];
            sb.append(randomChar);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(getRandomID("sql"));
    }



}
